home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / dfpp01.zip / RADIO.CPP < prev    next >
C/C++ Source or Header  |  1992-11-21  |  3KB  |  139 lines

  1. // ------------- radio.cpp
  2.  
  3. #include "radio.h"
  4. #include "desktop.h"
  5.  
  6. RadioButton::RadioButton(char *lbl, int lf, int tp, DFWindow *par)
  7.                 : TextBox(lf, tp, 1, strlen(lbl)+5, par)
  8. {
  9.     OpenWindow();
  10.     String lb("( ) ");
  11.     lb += lbl;
  12.     lb += " ";
  13.     SetText(lb);
  14. }
  15.  
  16. // ----------- common constructor code
  17. void RadioButton::OpenWindow()
  18. {
  19.     windowtype = RadioButtonWindow;
  20.     if (windowstate == CLOSED)
  21.         TextBox::OpenWindow();
  22.     SetColors();
  23.     setting = False;
  24. }
  25.  
  26. void RadioButton::CloseWindow()
  27. {
  28.     TextBox::CloseWindow();
  29. }
  30.  
  31. // -------- set the fg/bg colors for the window 
  32. void RadioButton::SetColors()
  33. {
  34.     colors.fg =
  35.     colors.sfg =
  36.     colors.ffg =
  37.     colors.hfg = Parent()->ClientFG(); 
  38.     colors.bg =
  39.     colors.sbg =
  40.     colors.fbg =
  41.     colors.hbg = Parent()->ClientBG();
  42.     shortcutfg = RED;
  43. }
  44.  
  45. void RadioButton::Paint()
  46. {
  47.     if (visible)    {
  48.         if (isEnabled())
  49.             WriteShortcutLine(0, colors.fg, colors.bg);
  50.         else 
  51.             WriteTextLine(0, colors.hfg, colors.hbg);
  52.     }
  53. }
  54.  
  55. Bool RadioButton::SetFocus()
  56. {
  57.     TextBox::SetFocus();
  58.     desktop.cursor().normalcursor();
  59.     desktop.cursor().SetPosition(Left()+1, Top());
  60.     desktop.cursor().Show();
  61.     return True;
  62. }
  63.  
  64. void RadioButton::ResetFocus()
  65. {
  66.     TextBox::ResetFocus();
  67.     desktop.cursor().Hide();
  68. }
  69.  
  70. void RadioButton::Keyboard(int key)
  71. {
  72.     if (key == ' ')
  73.         PushRadioButton();
  74.     else 
  75.         TextBox::Keyboard(key);
  76. }
  77.  
  78. void RadioButton::LeftButton(int mx, int my)
  79. {
  80.     if (ClientRect().Inside(mx,my))
  81.         PushRadioButton();
  82. }
  83.  
  84. void RadioButton::PushRadioButton()
  85. {
  86.     int ht = desktop.screen().Height();
  87.     DFWindow **rd = new DFWindow *[ht];
  88.     for (int i = 0; i < ht; i++)
  89.         rd[i] = NULL;
  90.     // ---- build a table of radio buttons at the same x coordiate
  91.     DFWindow *sib = Parent()->First();
  92.     while (sib != NULL)    {
  93.         if (sib->WindowType() == RadioButtonWindow)    {
  94.             if (sib->Left() == Left())    {
  95.                 int tp = sib->Top();
  96.                 if (tp < ht)
  97.                     rd[tp] = sib;
  98.             }
  99.         }
  100.         sib = sib->Next();
  101.     }
  102.     // ----- find the start of the radiobutton group
  103.     i = Top();
  104.     while (i >= 0 && rd[i] != NULL)
  105.         --i;
  106.     // ---- ignore everthing before the group
  107.     while (i >= 0)
  108.         rd[i--] = NULL;
  109.     // ----- find the end of the radiobutton group
  110.     i = Top();
  111.     while (i < ht && rd[i] != NULL)
  112.         i++;
  113.     // ---- ignore everthing past the group
  114.     while (i < ht)
  115.         rd[i++] = NULL;
  116.     // ------ release all the radio buttons in the group
  117.     for (i = 0; i < ht; i++)
  118.         if (rd[i] != NULL)
  119.             ((RadioButton *)rd[i])->ReleaseRadioButton();
  120.     delete [] rd;
  121.     // ----- set the chosen radio button
  122.     setting = True;
  123.     (*text)[1] = (char) 7;
  124.     Paint();
  125. }
  126.  
  127. void RadioButton::ReleaseRadioButton()
  128. {
  129.     setting = False;
  130.     (*text)[1] = ' ';
  131.     Paint();
  132. }
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.